<template>
  <div>
    <el-dialog title="品牌详情" :visible.sync="dialogFormVisible" width="65%" top="15vh">
      <el-form :model="form" :rules="rules">
        <el-row :gutter="20">
          <el-col :span="10" :offset="0">
            <el-form-item label="活动名称" prop="name" :label-width="formLabelWidth">
              <el-input v-model="form.name" size="mini" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="0">
            <el-form-item label="首字母" :label-width="formLabelWidth">
              <el-input v-model="form.firstLetter" size="mini" autocomplete="off" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="10" :offset="0">
            <el-form-item label="排序" :label-width="formLabelWidth">
              <el-input v-model="form.sort" size="mini" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="0">
            <el-form-item label="产品数量" :label-width="formLabelWidth">
              <el-input v-model="form.productCount" size="mini" autocomplete="off" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="10" :offset="0">
            <el-form-item label="是否展示" :label-width="formLabelWidth">
              <el-switch
                v-model="form.showStatus"
                :active-value="1"
                :inactive-value="0"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="0">
            <el-form-item label="是否成为品牌制造商" :label-width="formLabelWidth">
              <el-switch
                v-model="form.factoryStatus"
                :active-value="1"
                :inactive-value="0"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="10" :offset="0">
            <el-form-item label="上传专区大图" :label-width="formLabelWidth">
              <el-upload
                class="avatar-uploader"
                action="/lejuAdmin/material/uploadFileOss"
                :headers="{token}"
                :show-file-list="false"
                :on-success="handleAvatarSuccessLeft"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="bigPic" :src="bigPic" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="0">
            <el-form-item label="上传logo" :label-width="formLabelWidth">
              <el-upload
                class="avatar-uploader"
                action="/lejuAdmin/material/uploadFileOss"
                :headers="{token}"
                :show-file-list="false"
                :on-success="handleAvatarSuccessRight"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="logo" :src="logo" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit(form)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/MyAuth'
import { addBrand } from '@/api/product/brand/index'
export default {
  components: {

  },
  props: {},
  data() {
    return {
      dialogFormVisible: false, // 控制弹出框显示
      form: '',
      formLabelWidth: '200px',
      bigPic: '',
      logo: '',
      rules: {
        name: [
          { required: true, message: '请输入品牌名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    token() {
      return getToken()
    }
  },
  watch: {

  },
  created() {
    console.log(this.form)
  },
  mounted() {

  },
  methods: {
    openDialog() {
      this.dialogFormVisible = true
    },
    beforeAvatarUpload() {},
    handleAvatarSuccessLeft(res) { // 上传区域大图左
      console.log(res)
      const { data, message: errMsg, success } = res
      if (success) {
        const { fileUrl } = data
        this.bigPic = fileUrl
      } else {
        this.$message.error(errMsg)
      }
    },
    handleAvatarSuccessRight(res) { // 上传logo
      console.log(res)
      const { data, message: errMsg, success } = res
      if (success) {
        const { fileUrl } = data
        this.logo = fileUrl
      } else {
        this.$message.error(errMsg)
      }
    },
    submit(el) { // 点击确定(提交)
      el.bigPic = this.bigPic
      el.logo = this.logo
      console.log(el)

      this.$confirm('是否确认新增品牌?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        addBrand(el).then(res => {
          console.log(res)
          const { message: errMsg, success } = res
          if (success) {
            this.$message({
              message: '新增成功',
              type: 'success'
            })
          } else {
            this.$message.error(errMsg)
          }
          this.$emit('call')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
      this.dialogFormVisible = false

      // this.$emit('refrush', el)
    },
    cancel() { // 点击取消
      this.dialogFormVisible = false
    }
  }
}
</script>

<style scoped lang="scss">
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
